package com.hemaapp.yjnh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hemaapp.yjnh.bean.Bank;
import com.hemaapp.yjnh.bean.User;
import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.R;

import xtom.frame.util.XtomToastUtil;

/**
 * 编辑银行卡
 * */
public class BankEditActivity extends BaseActivity implements OnClickListener{

	private final static int REQUEST_TIXIAN = 0x12;//提现
	
	private TextView titleText;
	private ImageButton titleLeft;
	private Button titleRight;

	private LinearLayout layout_0;
	
	private TextView bankname;
	private LinearLayout layout_1;
	private TextView bankuser;
	private LinearLayout layout_2;
	private TextView bankcard;
	private LinearLayout layout_3;
	private TextView bankaddress;

	private boolean needtoCashAdd;
    private User user;
    String nameStr;
    String userStr;
    String cardStr;
    String addressStr;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_editcard);
        user = getApplicationContext().getUser();
		super.onCreate(savedInstanceState);
		initBank();
	}

	private void initBank() {
		if (user != null) {
			bankname.setText(user.getBankname());
			bankuser.setText(user.getBankuser());
			bankcard.setText(user.getBankcard());
			bankaddress.setText(user.getBankaddress());
		}
	}

	@Override
	protected void callBeforeDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case BANK_SAVE:
			showProgressDialog("请稍候");
			break;
		default:
			break;
		}
	}

	@Override
	protected void callAfterDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case BANK_SAVE:
			cancelProgressDialog();
			break;
		default:
			break;
		}
	}

	@Override
	protected void callBackForServerSuccess(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case BANK_SAVE:
			user.setBankname(nameStr);
			user.setBankuser(userStr);
			user.setBankcard(cardStr);
			user.setBankaddress(addressStr);
			if (needtoCashAdd) {// 如果是首次设置银行卡，跳转至提现页面
				Intent it = new Intent(mContext, Cash2BankActivity.class);
				startActivityForResult(it, REQUEST_TIXIAN);
			}
			finish();
			break;
		default:
			break;
		}

	}

	@Override
	protected void callBackForServerFailed(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case BANK_SAVE:
            XtomToastUtil.showShortToast(mContext,"保存失败,"+baseResult.getMsg());
			break;
		default:
			break;
		}
	}

	@Override
	protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case BANK_SAVE:
			XtomToastUtil.showShortToast(mContext,"保存失败，请稍后再试");
			break;
		}
	}

	@Override
	protected void findView() {
        titleLeft = (ImageButton) findViewById(R.id.title_left_btn);
        titleText = (TextView) findViewById(R.id.title_text);
        titleRight = (Button) findViewById(R.id.title_right_btn);

		layout_0 = (LinearLayout) findViewById(R.id.layout_0);
		bankname = (TextView) findViewById(R.id.bankname);
		layout_1 = (LinearLayout) findViewById(R.id.layout_1);
		bankuser = (TextView) findViewById(R.id.bankuser);
		layout_2 = (LinearLayout) findViewById(R.id.layout_2);
		bankcard = (TextView) findViewById(R.id.bankcard);
		layout_3 = (LinearLayout) findViewById(R.id.layout_3);
		bankaddress = (TextView) findViewById(R.id.bankaddress);
	}

	@Override
	protected void getExras() {
		needtoCashAdd = mIntent.getBooleanExtra("needtoCashAdd", false);
	}

	@Override
	protected void setListener() {
		titleText.setText("编辑提现账号");
		titleLeft.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		titleRight.setText("保存");
		titleRight.setVisibility(View.VISIBLE);
		titleRight.setOnClickListener(new SaveListener());

		layout_0.setOnClickListener(this);
		layout_1.setOnClickListener(this);
		layout_2.setOnClickListener(this);
		layout_3.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		Intent it;
		switch (v.getId()) {
		case R.id.layout_0:
			it = new Intent(mContext, BankActivity.class);
			startActivityForResult(it, 0);
			break;
		case R.id.layout_1:
			it = new Intent(mContext, SetInputActivity.class);
			it.putExtra("requestCode", 1);
			it.putExtra("name", "户主姓名");
            it.putExtra("value", user.getBankuser());
			startActivityForResult(it, 1);
			break;
		case R.id.layout_2:
			it = new Intent(mContext, SetInputActivity.class);
			it.putExtra("requestCode", 2);
			it.putExtra("name", "银行卡号");
            it.putExtra("value", user.getBankcard());
			startActivityForResult(it, 2);
			break;
		case R.id.layout_3:
			it = new Intent(mContext, SetInputActivity.class);
			it.putExtra("requestCode", 3);
			it.putExtra("name", "开户行名称");
            it.putExtra("value", user.getBankaddress());
			startActivityForResult(it, 3);
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode != RESULT_OK)
			return;
		switch (requestCode) {
		case 0:// 银行名称
			Bank bank = (Bank) data.getSerializableExtra("bank");
			bankname.setText(bank.getName());
			break;
		case 1:// 户主姓名
			String user = data.getStringExtra("backdata");
			bankuser.setText(user);
			break;
		case 2:// 银行卡号
			String card = data.getStringExtra("backdata");
			bankcard.setText(card);
			break;
		case 3:// 开户行名称
			String address = data.getStringExtra("backdata");
			bankaddress.setText(address);
			break;
		case REQUEST_TIXIAN:
			setResult(RESULT_OK);
			finish();
			break;
		}
	}

	private class SaveListener implements OnClickListener {

        @Override
		public void onClick(View v) {
            userStr = bankuser.getText().toString();
            nameStr = bankname.getText().toString();
            cardStr = bankcard.getText().toString();
            addressStr = bankaddress.getText().toString();
			if (isNull(nameStr)) {
				XtomToastUtil.showShortToast(mContext, "请选择一个银行");
				return;
			}
			if (isNull(userStr)) {
                XtomToastUtil.showShortToast(mContext, "请输入户主姓名");
				return;
			}
			if (isNull(cardStr)) {
                XtomToastUtil.showShortToast(mContext, "请输入银行卡号");
				return;
			}
			if (isNull(addressStr)) {
                XtomToastUtil.showShortToast(mContext, "请输入开户行名称");
				return;
			}
			String token = getApplicationContext().getUser().getToken();
			getNetWorker().bankSave(token, userStr, nameStr, cardStr, addressStr);
		}
	}

}
